import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver, EventEmitter } from '@angular/core';
import * as flatpickr from 'flatpickr';
import { zh } from "flatpickr/dist/l10n/zh.js";
import { DataService } from '../../../../../services/data.service'
import { environment } from "../../../../../../environments/environment";
import * as _ from "lodash";
import { Util } from '../../../../../common/util';
import { ViewSearchService } from './view.service';
@Component({
  selector: 'app-view-search',
  templateUrl: './view-search.component.html',
  styleUrls: ['./view-search.component.scss'],
  providers: [ViewSearchService]
})
export class ViewSearchComponent implements OnInit {

  //获取搜索条件字典值
  public dictionary: any = {
    RISK_RECORD_TYPE: [],//属性
    NX_SOURCE: [],//条款出处
    RISK_ACCOUNT_TYPE: [],//适用基金组类别
    RISK_CTRL_METHOD: [],//事中控制方法
    RISK_CONTROL_AFTER: [],//事后监控方法
    MONITOR_REMARK: [],//风险等级
    RISK_CW_CTRL: [],//是否场外系统可控
    RISK_HS_CONTROL: []//是否事中系统可控
  }
  //基金组列表
  public groupList: any = [];
  //查询列表getBody
  public getBody: any = {
    pageIndex: 1,
    pageSize: 50,
    sOprDateCondition: '',//时间点
    sValid: "", // sValid 是否有效
    sAccountName: '',//账户名称
    sFundType: '',//适用基金类别
    sFundGroupds: '',//适用基金组
    sItemType: '',//风控条目属性
    nNdId: '',// nNdId 风控条目编号
    sFileName: '',//文件名称
    sFileSource: '',//条款出处
    f1: '',//内容
    f2: '',//指标释义
    f3: '',//条目规定阈值
    f4: '',//事中控制方法
    f5: '',//事中系统控制阈值
    f6: '',//是否事中系统可控
    f7: '',//系统风控编号
    f8: '',//事中系统备注
    f9: '',//事后监控方法
    f10: '',//监控编号
    f11: '',//控制说明
    f12: '',//补偿控制
    f13:'',//风险等级
    f14: '',//监察部备注1
    f15: '',//监察部备注2
    f16: '',//场外系统阀值（新股，定增，转债）
    f17: '',//场外系统阀值（公增）
    f18: '',//是否场外系统可控
    f19: '',//场外分控编号
    f20: '',//场外系统备注 

  };
  public sFundType: any = [];
  public sFundGroupds: any = [];
  public sFileSourcelist: any = [];
  //基金代码展示转义中文
  public sFundGroupdsZh: any;
  constructor(
    public resolver: ComponentFactoryResolver,
    public dataService: DataService,
    public viewSearchService: ViewSearchService
  ) { }

  ngOnInit() {
    let that = this;
    // 多选下拉框
    window['$'](".select2").select2();
    //通过服务获取字典值
    this.dictionary = JSON.parse(localStorage.getItem("dictionaryList"));

    Util.loadSelect(this.dictionary.NX_SOURCE, 'NqdiiViewSearchFileSource');
    Util.loadSelect(this.dictionary.RISK_ACCOUNT_TYPE, 'NqdiiViewSearchFundType');
    this.fundGroupList();
    //初始化适用基金组多选框,因为使用的不是字典值，所以通用方法无法使用
    // setTimeout(() => {
    //   this.fundGroupsSelect2();
    // }, 500)
  }
  //初始化适用基金组多选框
  fundGroupsSelect2() {
    let that = this;
    var html = '';
    for (var i = 0; i < this.groupList.length; i++) {
      html += "<option value='" + this.groupList[i].sFgName + "'>" + this.groupList[i].sFgName + "</option>";
    }
    window['$']("#NqdiiViewSearchFundGroupds").html(html);

    window['$']('#NqdiiViewSearchFundGroupds').selectpicker({
      size: "8",
      actionsBox: "true",
      title: "请选择",
      selectAllText: "全选",
      deselectAllText: "取消全选",
      width: 182
    });
  }
  //获取基金组信息列表
  fundGroupList() {
    let that = this;
    let getBody = {
      sType: 0
    }
    this.viewSearchService.fundGroupList(getBody).subscribe(data => {
      if (data) {
        this.groupList = data;
        var html = '';
        for (var i = 0; i < this.groupList.length; i++) {
          html += "<option value='" + this.groupList[i].nFgId + "'>" + this.groupList[i].sFgName + "</option>";
        }
        window['$']("#NqdiiViewSearchFundGroupds").html(html);
    
        window['$']('#NqdiiViewSearchFundGroupds').selectpicker({
          size: "8",
          actionsBox: "true",
          title: "请选择",
          selectAllText: "全选",
          deselectAllText: "取消全选",
          width: 182
        });
      }
    })
  }
  searchBody() {
    console.log(this.sFundGroupds)
    this.getBody.sFundType = this.sFundType.join(',');
    this.getBody.sFundGroupds = this.sFundGroupds.join(',');
    this.getBody.sFileSource = this.sFileSourcelist.join(',');
    this.dataService.nQDIIViewSearchInfo = _.cloneDeep(this.getBody);
    console.log( this.dataService.nQDIIViewSearchInfo)
    this.dataService.nQDIIViewEmitter.emit('searchBody');
  }
  //风控条目编号正整数控制
  integerControl() {
    setTimeout(() => {
      this.getBody.nQdId = this.getBody.nQdId.replace(/[^0-9]/g, '');
    });
  }

}
